package com.spa.interfaces.admin;

import com.spa.application.command.admin.TicketAddCommand;
import com.spa.application.command.admin.TicketEditCommand;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.TicketDTO;
import com.spa.application.dto.admin.TicketExportDTO;
import com.spa.application.dto.admin.TicketPickRecordDTO;
import com.spa.application.dto.admin.TicketPickRecordExportDTO;
import com.spa.application.query.admin.TicketPickRecordQuery;
import com.spa.application.query.admin.TicketQuery;
import com.spa.application.service.admin.AdminTicketApplicationService;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.interfaces.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("admin/ticket")
public class AdminTicketController extends BaseController {

    @Autowired
    private AdminTicketApplicationService adminTicketApplicationService;

    @PostMapping("add")
    public ResponseMessage add(@Validated @RequestBody TicketAddCommand command) {
        adminTicketApplicationService.addTicket(command);
        return responseHelper.success();
    }

    @PostMapping("page")
    public ResponseMessage<PageResult<TicketDTO>> page(@RequestBody TicketQuery query) {
        return responseHelper.success(adminTicketApplicationService.page(query));
    }

    @PostMapping("export")
    public void export(HttpServletResponse response,@RequestBody TicketQuery query) throws Exception {
        exportQueryResult(response,adminTicketApplicationService.list(query),"优惠券列表","优惠券列表", TicketExportDTO.class);
    }

    @PostMapping("offline")
    public ResponseMessage offline(@RequestBody TicketEditCommand command) {
        adminTicketApplicationService.offline(command);
        return responseHelper.success();
    }

    @PostMapping("pageRecord")
    public ResponseMessage<PageResult<TicketPickRecordDTO>> pageRecord(@RequestBody TicketPickRecordQuery query) {
        return responseHelper.success(adminTicketApplicationService.pagePickRecord(query));
    }

    @PostMapping("record/export")
    public void recordExport(HttpServletResponse response,@RequestBody TicketPickRecordQuery query) throws Exception {
        exportQueryResult(response,adminTicketApplicationService.receiveList(query),"优惠券领取记录","优惠券领取记录", TicketPickRecordExportDTO.class);
    }

}
